/**
 * Created by alice on 2017/5/22 0022.
 */
if (navigator.appName.indexOf("Microsoft Internet") == -1) {
    onVLCPluginReady()
}
else if (document.readyState == 'complete') {
    onVLCPluginReady();
}
else {
    /* Explorer loads plugins asynchronously */
    document.onreadystatechange = function () {
        if (document.readyState == 'complete') {
            onVLCPluginReady();
        }
    }
}

var pro_bar = progress.init('#pro_bar', {});
var vlc = getVLC("vlc");
pro_bar.pause = function(){
    if (vlc.input.state==3){
        vlc.playlist.togglePause();
    }
};
pro_bar.play = function(){
    if (vlc.input.state==4){
        vlc.playlist.togglePause();
        onPlaying();
    }
};
pro_bar.changeVolume = function(volume){
    vlc.audio.volume = volume*100;
};
pro_bar.changeCurrentTime = function(currentTime){
    vlc.input.time=currentTime*1000;
};
pro_bar.browserFullscreen =function () {
    vlc.video.toggleFullscreen();
};

function onPlaying(){
    var vlc = getVLC("vlc");
    if( vlc ) {
        var mediaLen = vlc.input.length;
        if (mediaLen>0){
            console.log(mediaLen);
            pro_bar.loadeddata(mediaLen/1000,vlc.audio.volume/100);
            change_pro();
        }
    }
}

// JS VLC API callbacks
function handleMediaPlayerMediaChanged() {
    var vlc = getVLC("vlc");
    if (vlc) {
        var mediaLen = vlc.input.length;
        if (mediaLen>0){
            console.log(mediaLen);
            pro_bar.loadeddata(mediaLen/1000,vlc.audio.volume/100);
            change_pro();
        }
    }
}
//更新正在播放的时间
function handle_MediaPlayerTimeChanged(time) {
    var vlc = getVLC("vlc");
    if (vlc) {
        var mediaLen = vlc.input.length;
        if (mediaLen > 0) {
            pro_bar.timeupdate(time/1000);
        }
    }
}

function handle_MediaPlayerNothingSpecial() {
    // document.getElementById("state").innerHTML = "Idle...";
}

function handle_MediaPlayerOpening() {
    // onOpen();
}

function handle_MediaPlayerBuffering(val) {
    // document.getElementById("info").innerHTML = val + "%";
}

function handle_MediaPlayerPlaying() {
    onPlaying();
}

function handle_MediaPlayerPaused() {
    onPause();
}

function handle_MediaPlayerStopped() {
//     onStop();
}

function handle_MediaPlayerForward() {
    // document.getElementById("state").innerHTML = "Forward...";
}

function handle_MediaPlayerBackward() {
    // document.getElementById("state").innerHTML = "Backward...";
}

function handle_MediaPlayerEndReached() {
    // onEnd();
}

function handle_MediaPlayerEncounteredError() {
    // onError();
}

function handle_MediaPlayerPositionChanged(val) {
    // set javascript slider to correct position
}

function handle_MediaPlayerSeekableChanged(val) {
    // setSeekable(val);
}

function handle_MediaPlayerPausableChanged(val) {
    // setPauseable(val);
}

function handle_MediaPlayerTitleChanged(val) {
}

function handle_MediaPlayerLengthChanged(val) {
    //setMediaLength(val);
}

function onPause() {
}

function getVLC(name) {
    if (window.document[name]) {
        return window.document[name];
    }
    if (navigator.appName.indexOf("Microsoft Internet") == -1) {
        if (document.embeds && document.embeds[name])
            return document.embeds[name];
    }
    else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
    {
        return document.getElementById(name);
    }
}

// VLC Plugin
function onVLCPluginReady() {
    registerVLCEvent("MediaPlayerMediaChanged", handleMediaPlayerMediaChanged);
    registerVLCEvent("MediaPlayerNothingSpecial", handle_MediaPlayerNothingSpecial);
    registerVLCEvent("MediaPlayerOpening", handle_MediaPlayerOpening);
    registerVLCEvent("MediaPlayerBuffering", handle_MediaPlayerBuffering);
    registerVLCEvent("MediaPlayerPlaying", handle_MediaPlayerPlaying);
    registerVLCEvent("MediaPlayerPaused", handle_MediaPlayerPaused);
    registerVLCEvent("MediaPlayerStopped", handle_MediaPlayerStopped);
    registerVLCEvent("MediaPlayerForward", handle_MediaPlayerForward);
    registerVLCEvent("MediaPlayerBackward", handle_MediaPlayerBackward);
    registerVLCEvent("MediaPlayerEndReached", handle_MediaPlayerEndReached);
    registerVLCEvent("MediaPlayerEncounteredError", handle_MediaPlayerEncounteredError);
    registerVLCEvent("MediaPlayerTimeChanged", handle_MediaPlayerTimeChanged);
    registerVLCEvent("MediaPlayerPositionChanged", handle_MediaPlayerPositionChanged);
    registerVLCEvent("MediaPlayerSeekableChanged", handle_MediaPlayerSeekableChanged);
    registerVLCEvent("MediaPlayerPausableChanged", handle_MediaPlayerPausableChanged);
    registerVLCEvent("MediaPlayerTitleChanged", handle_MediaPlayerTitleChanged);
    registerVLCEvent("MediaPlayerLengthChanged", handle_MediaPlayerLengthChanged);
}

function close(){
    unregisterVLCEvent("MediaPlayerMediaChanged", handleMediaPlayerMediaChanged);
    unregisterVLCEvent("MediaPlayerNothingSpecial", handle_MediaPlayerNothingSpecial);
    unregisterVLCEvent("MediaPlayerOpening", handle_MediaPlayerOpening);
    unregisterVLCEvent("MediaPlayerBuffering", handle_MediaPlayerBuffering);
    unregisterVLCEvent("MediaPlayerPlaying", handle_MediaPlayerPlaying);
    unregisterVLCEvent("MediaPlayerPaused", handle_MediaPlayerPaused);
    unregisterVLCEvent("MediaPlayerStopped", handle_MediaPlayerStopped);
    unregisterVLCEvent("MediaPlayerForward", handle_MediaPlayerForward);
    unregisterVLCEvent("MediaPlayerBackward", handle_MediaPlayerBackward);
    unregisterVLCEvent("MediaPlayerEndReached", handle_MediaPlayerEndReached);
    unregisterVLCEvent("MediaPlayerEncounteredError", handle_MediaPlayerEncounteredError);
    unregisterVLCEvent("MediaPlayerTimeChanged", handle_MediaPlayerTimeChanged);
    unregisterVLCEvent("MediaPlayerPositionChanged", handle_MediaPlayerPositionChanged);
    unregisterVLCEvent("MediaPlayerSeekableChanged", handle_MediaPlayerSeekableChanged);
    unregisterVLCEvent("MediaPlayerPausableChanged", handle_MediaPlayerPausableChanged);
    unregisterVLCEvent("MediaPlayerTitleChanged", handle_MediaPlayerTitleChanged);
    unregisterVLCEvent("MediaPlayerLengthChanged", handle_MediaPlayerLengthChanged);
}

function registerVLCEvent(event, handler) {
    var vlc = getVLC("vlc");
    if (vlc) {
        if (vlc.attachEvent) {
            // Microsoft
            vlc.attachEvent(event, handler);
        } else if (vlc.addEventListener) {
            // Mozilla: DOM level 2
            vlc.addEventListener(event, handler, true);
        } else {
            // DOM level 0
            eval("vlc.on" + event + " = handler");
        }
    }
}

function unregisterVLCEvent(event, handler) {
    var vlc = getVLC("vlc");

    if (vlc) {
        if (vlc.detachEvent) {
            // Microsoft
            vlc.detachEvent(event, handler);
        } else if (vlc.removeEventListener) {
            // Mozilla: DOM level 2
            vlc.removeEventListener(event, handler, true);
        } else {
            // DOM level 0
            eval("vlc.on" + event + " = null");
        }
    }
}

function change_pro() {
    var play = $('.audioplayer-playpause').find('a');
    console.log(play.html());
    if(play.html()=='Play'){
        play.click();
    }
}
